package com.sky.controller.user;


import com.sky.dto.PageQuery;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.impl.BookServiceImpl;
import com.sky.vo.BookVO;
import com.sky.vo.NoteVO;
import com.sky.vo.ReviewVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "C端-图书的相关接口")
@Slf4j
@RequestMapping("/user/book")
public class BookController {
    @Autowired
    private BookServiceImpl bookService;

    /**
     * 根据三级标题的id获取图书列表
     *
     * @param tId
     * @return
     */
    @GetMapping("/list/{id}")
    @ApiOperation("根据三级标题的id获取图书列表")
    public Result<List<BookVO>> list(@PathVariable("id") Integer tId) {
        log.info("三级标题的id:{}", tId);
        List<BookVO> bookList = bookService.list(tId);
        return Result.success(bookList);
    }

    /**
     * 根据图书id获取图书详情
     *
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    @ApiOperation("根据图书id获取图书信息")
    public Result<BookVO> detail(@PathVariable String id) {
        log.info("图书id:{}", id);
        BookVO bookVO = bookService.detail(id);
        return Result.success(bookVO);
    }

    /**
     * 根据查询关键词获取图书列表
     *
     * @param pageQuery
     * @return
     */
    @GetMapping("/search")
    @ApiOperation("根据关键词获取图书信息")
    public Result<PageResult> search(PageQuery pageQuery) {
        log.info("搜索关键词:{}", pageQuery);
        PageResult result = bookService.search(pageQuery);
        return Result.success(result);
    }

    /**
     * 获取用户书架信息
     *
     * @return
     */
    @GetMapping("/bookcase")
    @ApiOperation("根据用户id获取书架图书列表")
    public Result<List<BookVO>> bookcase() {
        List<BookVO> bookList = bookService.bookcase();
        return Result.success(bookList);
    }

    /**
     * 根据图书id添加到书架
     *
     * @param bId
     * @return
     */
    @PostMapping("/add/{id}")
    @ApiOperation("根据图书id添加到书架")
    public Result addToBookCase(@PathVariable("id") String bId) {
        log.info("要添加到书架的图书id:{}", bId);
        bookService.addToBookCase(bId);
        return Result.success();
    }

    /**
     * 根据图书id移除书架
     *
     * @param bIds
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation("根据图书id移除书架")
    public Result removeFromBookCase(@RequestParam("ids") String[] bIds) {
        bookService.removeFromBookCase(bIds);
        return Result.success();
    }

    /**
     * 获取用户做过笔记和书评
     *
     * @return
     */
    @GetMapping("/note-review")
    @ApiOperation("获取用户做过的笔记和书评")
    public Result<List<BookVO>> getNoteReview() {
        List<BookVO> list = bookService.getNoteReview();
        return Result.success(list);
    }

    /**
     * 根据图书id获取用户做过的笔记
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}/notes")
    @ApiOperation("根据图书id获取与用户做过的笔记")
    public Result<List<NoteVO>> getBookNotes(@PathVariable("id") String id) {
        List<NoteVO> notes = bookService.getBookNotes(id);
        return Result.success(notes);
    }
    /**
     * 根据图书id获取用户的评论
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}/reviews")
    @ApiOperation("根据图书id获取用户的书评")
    public Result<List<ReviewVO>> getBookReviews(@PathVariable("id") String id) {
        List<ReviewVO> reviews = bookService.getBookReviews(id);
        return Result.success(reviews);
    }

}
